package com.sf.dlb.mapper.dao;

import com.sf.dlb.jpa.model.UserMessageStateInfo;
import com.sf.dlb.mapper.entity.UserMessageEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper;

import java.util.List;

/**
 * 用户消息
 * Created by tanjing on 2019/4/28.
 */
@Mapper
public interface UserMessageMapper {
    /**
     * 查询-消息列表
     */
    @Select("SELECT a.*,GROUP_CONCAT(m.user_id) AS 'userIds' FROM user_message a LEFT JOIN user_message_state m ON a.id=m.message_id WHERE a.type=#{type} AND a.del_flag='0' GROUP BY a.id ORDER BY a.create_date desc")
    List<UserMessageEntity> findbyAllTypeMessage(@Param("type") String type);
    /**
     * 查询-消息列表
     */
    @Select("SELECT a.*,GROUP_CONCAT(m.user_id) AS 'userIds' FROM user_message a LEFT JOIN user_message_state m ON a.id=m.message_id WHERE a.del_flag='0' GROUP BY a.id ORDER BY a.create_date desc")
    List<UserMessageEntity> findbyAllMessage();
    /**
     * 根据查询-用户id
     */
    @Select("select s.id,a.title,a.content,a.type,a.sort,s.state,a.create_date from user_message a LEFT JOIN user_message_state s ON a.id=s.message_id where s.user_id=#{userId} and a.del_flag=#{delFlag} order by a.update_date desc")
    List<UserMessageEntity> findByUserIdAndDelFlag(@Param("userId") String userId, @Param("delFlag")String delFlag);
    /**
     * 根据查询-用户id，type
     */
    @Select("select s.id,a.title,a.content,a.type,a.sort,s.state,a.create_date from user_message a LEFT JOIN user_message_state s ON a.id=s.message_id where s.user_id=#{userId} and a.type=#{type} and a.del_flag=#{delFlag} order by a.update_date desc")
    List<UserMessageEntity> findByUserIdAndTypeAndDelFlag(@Param("userId") String userId, @Param("type")String type, @Param("delFlag")String delFlag);
    /**
     * 根据查询-用户id，type,state
     */
    @Select("select s.*,a.* from user_message a LEFT JOIN user_message_state s ON a.id=s.message_id where s.user_id=#{userId} and a.type=#{type} and s.state=#{state} and a.del_flag=#{delFlag} order by a.update_date desc")
    List<UserMessageStateInfo> findByUserIdAndTypeAndStateAndDelFlag(@Param("userId") String userId, @Param("type")String type, @Param("state")String state, @Param("delFlag")String delFlag);

}
